package com.primb.androidlibs.update.utils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

/**
 * @author Chen
 * @date 2017/3/9
 */

public abstract class DownloadReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        String tag = bundle.getString("type");
        if ("err".equals(tag)) {
            String err = bundle.getString("err");
            downloadFail(err);
        } else if ("doing".equals(tag)) {
            int progress = bundle.getInt("progress");
            downloading(progress);
            if (progress == 100) {
                downloadComplete();
            }
        }
    }

    /**
     * 下载完成
     */
    protected abstract void downloadComplete();

    /**
     * 下载中
     * @param progress 下载进度
     */
    protected abstract void downloading(int progress);

    /**
     * 下载失败
     * @param e 失败原因
     */
    protected abstract void downloadFail(String e);
}
